const _http = require('http');
const _url = require('url')
const _config = require('./config');
const _video = require('./category/video');
const Router = require('router')
const finalhandler = require('finalhandler');//友好处理找不到页面访问不到URL的异常和错误

var router = Router();

router.get("**", (req, resp, next) => {
    let start = Date.now()
    resp.on("finish", () => { console.log(`response: ${Date.now() - start}ms`) })
    next()
})

router.get('/b/:groupID/:videoID', function (req, res) {
    _video.getData(req.params.groupID, req.params.videoID).then((data) => {
        res.setHeader("content-type", "application/json")
        res.end(JSON.stringify(data))
    }).catch((e) => {
        console.log(e)
        res.statusCode = 500
        res.end()
    })
});

var server = _http.createServer(function (req, res) {
    router(req, res, finalhandler(req, res))
})

server.listen(9999)